#include "MovingAnimator.h"
#include "Popeye.h"

MovingAnimator::MovingAnimator (void): sprite((Sprite*) 0), anim((MovingAnimation*) 0) {}

void MovingAnimator::Progress (timestamp_t currTime) {
	if (currTime > lastTime && currTime - lastTime >= anim->GetDelay()) {
		((Popeye *)(sprite))->Move(anim->GetDx(), anim->GetDy());
		sprite->SetFlipH(anim->GetFlipH());
		sprite->SetFlipV(anim->GetFlipV());
		if (!anim->GetContinuous()) {
			state = ANIMATOR_FINISHED;
			NotifyStopped();
		}
		else {
			lastTime += anim->GetDelay();
			Progress(currTime);  // Recursion (make it a loop)
		}
	}
}
	
void MovingAnimator::Start (Sprite* s, MovingAnimation* a, timestamp_t t) {
	sprite	= s;
	anim 	= a;
	lastTime 	= t;
	state 	= ANIMATOR_RUNNING;
}